// d:\byte\tiktok-applet-v3 - 智慧天气 - 副本\pages\mybazi\mybazi.js
const model = require('../../public/model/env.js');

const url = model.api_pro_url(); // 线上环境
const key = model.key();
// const appid = model.appid();
const userid = tt.getStorageSync('userid');

// const date = new Date()
const years = []
const months = []
const days = []
let date = new Date(2010, 0, 1); // 创建一个表示2010年1月1日
for (let i = 1940; i <= date.getFullYear(); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}

Page({
  data: {
    mybaziresult: '暂未设置 生辰八字 不建议儿童使用',
    gender: '',
    // lassName: 'containerdark',
    title: '生辰八字',
    mybazi: '',
    years: years,
    year: date.getFullYear(),
    months: months,
    month: 2,
    days: days,
    day: 2,
    hour: 12,
    minute: 0,
    year: date.getFullYear(),
    value: [9999, 1, 1],
    immediateChange: false,
    minutes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59], // 分钟数组
    hours: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23], // 小时数组
  },
  genderChange: function (e) {
    console.log('选择的性别为', e.detail.value);
    this.setData({
      gender: e.detail.value
    });
  },
  bindChange(e) {
    const val = e.detail.value
    console.log('Detail value of bindChange: ', val);
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
      day: this.data.days[val[2]],
      hour: this.data.hours[val[3]],
      minute: this.data.minutes[val[4]],
    })
  },
  bindStart(e) {
    console.log('Detail value of bindpickstart: ', e.detail.value);
  },
  bindEnd(e) {
    console.log('Detail value of bindpickend: ', e.detail.value);
  },
  switchChange(e) {
    this.setData({
      immediateChange: e.detail.value,
    })
  },
  bazi: function () {
    const requestUrl = `${url}/index.php/api/points_log.php`;

    const options = {
      url: requestUrl,
      data: {
        user_id: userid,
        key: key
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      dataType: 'json',
    };
    console.log(options);
    tt.request({
      ...options,
      success: (res) => {
        const data = res.data;
        console.log(data.data);
        if (data.code == 200) {
          that.setData({
            orderList: data.data,
          });

        }
      },
      fail: (err) => {
      }
    });
  },
  bazi: function () {
    const mybazi = this.data.year + '年-' + this.data.month + '月-' + this.data.day + '日 ' + this.data.hour + ':' + this.data.minute + '-性别：' + this.data.gender;
    const requestUrl = `${url}/index.php/api/bazi.php`;
    const bazijiaoshao = {
      year: this.data.year,
      month: this.data.month,
      day: this.data.day,
      hour: this.data.hour,
      minute: this.data.minute,
      gender: this.data.gender,
    };
    const bazijiaoshaoString = JSON.stringify(bazijiaoshao);
    tt.setStorageSync('bazijiaoshao', bazijiaoshaoString);
    const options = {
      url: requestUrl,
      data: {
        user_id: userid,
        key: key,
        bazi: mybazi,
        bazijiaoshao: bazijiaoshaoString,
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      dataType: 'json',
    };
    console.log(options);
    tt.request({
      ...options,
      success: (res) => {
        const data = res.data;
        console.log(data);
        if (data.code == 200) {
          this.setData({
            mybaziresult: mybazi,

          });
          tt.showModal({

            title: '保存中',
            content: '正在提交数据，请稍后...',
          });

        } else {
          tt.showModal({

            title: '保存失败',
            content: '提交数据失败，请稍后重试...',
          });
        }
      },
      fail: (err) => {
      }
    });
  },
  baziresult: function () {
    const requestUrl = `${url}/index.php/api/bazi_list.php`;

    const options = {
      url: requestUrl,
      data: {
        user_id: userid,
        key: key,
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      dataType: 'json',
    };
    // console.log(options);
    tt.request({
      ...options,
      success: (res) => {
        const data = res.data;
        const bazijiaoshao = JSON.parse(data.data.bazijiaoshao);
        console.log(bazijiaoshao);
        if (data.data.bazi != '') {
          this.setData({
            mybaziresult: data.data.bazi,
            year: bazijiaoshao.year,
            month: bazijiaoshao.month,
            day: bazijiaoshao.day,
            hour: bazijiaoshao.hour,
            minute: bazijiaoshao.minute,
            gender: bazijiaoshao.gender,
          });
          tt.showModal({

            title: '查询到',
            content: '查询到您的生辰八字：' + bazijiaoshao.year + '年-' + bazijiaoshao.month + '月-' + bazijiaoshao.day + '日 ' + bazijiaoshao.hour + ':' + bazijiaoshao.minute + '-性别：' + bazijiaoshao.gender,
          });

        }
      },
      fail: (err) => {
      }
    });
  },
  sevbazi() {

    this.bazi();

  },
  onLoad: function (options) {
    this.baziresult();


    // 页面加载,标题设置
    tt.setNavigationBarTitle({
      title: this.data.title,
    });
    tt.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000000',
    });
    // 页面加载,标题设置结束
  }
})